package cn.itcast.sys.controller;


import cn.itcast.annotation.Permission;
import cn.itcast.constant.PermissionConstant;
import cn.itcast.context.BaseContext;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import cn.itcast.sys.dto.EmployeeDTO;
import cn.itcast.sys.dto.EmployeeLoginDTO;
import cn.itcast.sys.dto.EmployeePageQueryDTO;
import cn.itcast.sys.pojo.Employee;
import cn.itcast.sys.service.EmployeeService;
import cn.itcast.users.dto.PasswordEditDTO;
import cn.itcast.users.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
@Permission(value = PermissionConstant.EMPLOYEE_MANAGEMENT_PERMISSIONS)
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    /**
     * 员工登录
     *
     * @param dto
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO dto){
        log.info("员工登录:{}",dto);
        Result<EmployeeLoginVO> result = employeeService.login(dto);

        return result;
    }

    /**
     * 新增员工
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增员工")
    @PostMapping
    public Result add(@RequestBody EmployeeDTO dto){
        log.info("新增员工:{}",dto);
        employeeService.add(dto);

        return Result.success();
    }

    /**
     * 员工分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public Result<PageBean> pageQuery(EmployeePageQueryDTO dto){
        log.info("查询员工：{}",dto);
        PageBean pageBean = employeeService.pageQuery(dto);

        return Result.success(pageBean);
    }


    /**
     * 启用、禁用员工账号
     *
     * @param status
     * @return
     */
    @ApiOperation("启用、禁用员工账号")
    @PostMapping("/status/{status}")
    public Result startOrStop(Long id, @PathVariable Integer status){
        log.info("启动、禁用员工账号：{},{}",id,status);
        employeeService.startOrStop(id,status);

        return Result.success();
    }

    /**
     * 编辑员工信息
     *
     * @param dto
     * @return
     */
    @ApiOperation("编辑员工信息")
    @PutMapping
    public Result update(@RequestBody EmployeeDTO dto){
        log.info("编辑员工信息:{}",dto);
        employeeService.update(dto);
        return Result.success();
    }

    /**
     *根据id查询员工
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询员工")
    @GetMapping("{id}")
    public Result<Employee> getById(@PathVariable Integer id){
        log.info("根据id查询员工:{}",id);
        Employee employee = employeeService.getById(id);

        return Result.success(employee);
    }

    /**
     * 修改密码
     *
     * @param dto
     * @return
     */
    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result<String> updatePassword(@RequestBody PasswordEditDTO dto){
        log.info("修改密码：{}",dto);
        employeeService.updatePassword(dto);
        return Result.success();
    }

    /**
     * 退出登录
     *
     * @return
     */
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result logout(){
        log.info("退出登录！退出id：{}", BaseContext.getCurrentId());
        BaseContext.removeCurrentId();
        return Result.success();
    }
}
